﻿var chatArray = [];
//var carArray = [];

//carArray.push({
//    model: 'sorento',
//    make: 'kia',
//    year: 2012,
//    color: 'green',
//    misc: 'sports edition'
//});

//carArray.push({
//    model: '325',
//    make: 'bmw',
//    year: 2008,
//    color: 'bling',
//    misc: 'awesome!'
//});

//for (var i = 0; i < carArray.length; i++) {
//    var car = carArray[i];
//    console.log('Here is car number ' + (i + 1));
//    for (var p in car) {
//        console.log(p + ': ' + car[p]);
//    }
//}

var getChatMessages = function (callback) {
    var req = new XMLHttpRequest();
    req.open('GET', 'https://codercamps-chatapp.firebaseio.com/.json');
    req.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            var obj = JSON.parse(this.response);

            for (var name in obj) {
                obj[name].id = name;
                chatArray.push(obj[name]);
            }

            if (callback && typeof (callback) === 'function') {
                callback();
            }
        }
    }

    req.send();
}

var displayResult = function () {
    var html = '';

    for (var message in chatArray) {
        html += '<ul>';
        html += '<li>Username: ' + chatArray[message].userName + '</li>';
        html += '<li>Text: ' + chatArray[message].textArea + '</li>';
        html += '</ul>';
    }

    document.getElementById('result').innerHTML = html;
}

getChatMessages(function () {
    displayResult();
});